package controller;

import com.alibaba.fastjson.JSON;
import service.ClockRuleService;
import service.impl.ClockRuleServiceImpl;
import entity.ClockRule;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/clockRuleController")
public class ClockRuleController extends HttpServlet {
    private ClockRuleService clockRuleService = new ClockRuleServiceImpl();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        String op = req.getParameter("op");

        try {
            switch (op) {
                case "findAll":
                    List<ClockRule> rules = clockRuleService.findAll();
                    out.write(JSON.toJSONString(rules));
                    break;
                case "findById":
                    Integer id = Integer.parseInt(req.getParameter("id"));
                    ClockRule rule = clockRuleService.findById(id);
                    out.write(JSON.toJSONString(rule));
                    break;
                case "save":
                    ClockRule newRule = JSON.parseObject(req.getParameter("rule"), ClockRule.class);
                    boolean saveResult = clockRuleService.save(newRule);
                    out.write(JSON.toJSONString(saveResult ? "保存成功" : "保存失败"));
                    break;
                case "update":
                    ClockRule updateRule = JSON.parseObject(req.getParameter("rule"), ClockRule.class);
                    boolean updateResult = clockRuleService.update(updateRule);
                    out.write(JSON.toJSONString(updateResult ? "更新成功" : "更新失败"));
                    break;
                case "delete":
                    Integer deleteId = Integer.parseInt(req.getParameter("id"));
                    boolean deleteResult = clockRuleService.delete(deleteId);
                    out.write(JSON.toJSONString(deleteResult ? "删除成功" : "删除失败"));
                    break;
                case "toggleStatus":
                    Integer toggleId = Integer.parseInt(req.getParameter("id"));
                    Boolean active = Boolean.parseBoolean(req.getParameter("active"));
                    boolean toggleResult = clockRuleService.toggleStatus(toggleId, active);
                    out.write(JSON.toJSONString(toggleResult ? "状态更新成功" : "状态更新失败"));
                    break;
                default:
                    out.write(JSON.toJSONString("无效操作"));
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            out.write(JSON.toJSONString("系统错误: " + e.getMessage()));
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}